﻿var ActiveIndex=0;

function ClientAddVote()
{
    Url=Portal.SODC.WebSite.Vote.GetAddNewUrl().value;
    window.showModalDialog(Url, "", "dialogHeight: 600px; dialogWidth: 700px; dialogTop: 10px; dialogLeft: 150px; edge: Raised; center: Yes; help: No; scroll: Yes; status: Yes;"); 
    value=Portal.SODC.WebSite.Vote.GetHtml().value;
    if(value.indexOf("ERROR")==-1)
    {
        document.getElementById("divVoteMain").innerHTML=value;
    }
}

function ClientSaveAddVote()
{
    language=document.getElementById("divSelectLanguage").options[document.getElementById("divSelectLanguage").selectedIndex].value;
    Subject=document.getElementById("txtSubject").value;
    VoteItems=Portal.SODC.WebSite.AddVote.GetVoteItemsObject().value;
    for(var Index=0;Index<VoteItems.length;Index++)
    {
        VoteItems[Index].VoteItemName=document.getElementById("txtItemName_"+Index.toString()).value;
        VoteItems[Index].IndexOrder=Index+1;
    }
    retValue = Portal.SODC.WebSite.AddVote.ServerSideAddVote(language,null,Subject,VoteItems).value;
    if(retValue.indexOf("ERROR")!=-1)
    {
        alert(retValue);
        return;
    }
    alert(retValue);
    window.close();
}


function ClientDeleteVote(VoteId)
{
    Msg=Portal.SODC.WebSite.Vote.GetConfirmDeleteMsg().value;
    if(confirm(Msg)==false)return;
    RetValue = Portal.SODC.WebSite.Vote.ServerSideDeleteVote(VoteId).value;
    if(RetValue.indexOf("ERROR")!=-1)
    {
        alert(RetValue);
        return;
    }
    alert(RetValue);
    
    value=Portal.SODC.WebSite.Vote.GetHtml().value;
    if(value.indexOf("ERROR")==-1)
    {
        document.getElementById("divVoteMain").innerHTML=value;
    }
}

function ShowClientUpdateUrlForm(VoteId)
{
    Url=Portal.SODC.WebSite.Vote.GetShowUpdateForm(VoteId).value;
    if(Url.indexOf("ERROR")!=-1)
    {
        alert(Url);
        return;
    }
    window.showModalDialog(Url, null, 'dialogHeight: 600px; dialogWidth: 700px; dialogTop: 10px; dialogLeft: 150px; edge: Raised; center: Yes; help: No; scroll: Yes; status: Yes;'); 
    value=Portal.SODC.WebSite.Vote.GetHtml().value;
    if(value.indexOf("ERROR")==-1)
    {
        document.getElementById("divVoteMain").innerHTML=value;
    }
}

function ShowFormAppendLink(VoteId)
{
    Url=Portal.SODC.WebSite.UpdateVote.GetShowAppendForm(VoteId).value;
    if(Url.indexOf("ERROR")!=-1)
    {
        alert(Url);
        return;
    }
    window.showModalDialog(Url, null, 'dialogHeight: 600px; dialogWidth: 700px; dialogTop: 10px; dialogLeft: 150px; edge: Raised; center: Yes; help: No; scroll: Yes; status: Yes;'); 
    language=document.getElementById("divSelectLanguage").options[document.getElementById("divSelectLanguage").selectedIndex].value;
    
    value=Portal.SODC.WebSite.UpdateVote.GetHtml(language,VoteId).value;
    if(value.indexOf("ERROR")==-1)
    {
        document.getElementById("divFormContent").innerHTML=value;
    }
}

function ClientSaveUpdateVote(VoteId)
{
    language=document.getElementById("divSelectLanguage").options[document.getElementById("divSelectLanguage").selectedIndex].value;
    Subject=document.getElementById("txtVote").value;
    VoteItems=Portal.SODC.WebSite.UpdateVote.GetVoteItemsObject().value;
    for(var Index=0;Index<VoteItems.length;Index++)
    {
        if(document.getElementById("txtItemName_"+Index.toString())!=null)
        {
            if(document.getElementById("txtVoteItemId_"+Index.toString())!=null)
            {
                VoteItems[Index].VoteItemId=document.getElementById("txtVoteItemId_"+Index.toString()).value;
            }
            VoteItems[Index].VoteItemName=document.getElementById("txtItemName_"+Index.toString()).value;
            VoteItems[Index].IndexOrder=document.getElementById("txtIndexOrder_"+Index.toString()).value;
        }
    }
    RetValue = Portal.SODC.WebSite.UpdateVote.ServerSideUpdateVote(language,VoteId,Subject,VoteItems).value;
    if(RetValue.indexOf("ERROR")!=-1)
    {
        alert(RetValue);
        return;
    }
    alert(RetValue);
    //window.close();
}

function AppendNewRow()
{
    document.getElementById("divContentItems").innerHTML= document.getElementById("divContentItems").innerHTML+Portal.SODC.WebSite.AddVote.ServerSideNewLinkItem(ActiveIndex).value;
    ActiveIndex++;
    alert(ActiveIndex);
}

function RemoveItem(Index)
{
    document.getElementById("divRowItem_"+Index).innerHTML="";
    Html=document.getElementById("divContentItems").innerHTML;
    document.getElementById("divContentItems").innerHTML=Portal.SODC.WebSite.AddVote.ProcessHtml(Html,Index).value;
}


function DetectLanguageBody(VoteId)
{
    language=document.getElementById("divSelectLanguage").options[document.getElementById("divSelectLanguage").selectedIndex].value;
    value=Portal.SODC.WebSite.UpdateVote.GetHtml(language,VoteId).value;
    if(value.indexOf("ERROR")!=-1)
    {
        alert(value);
        return;
    }
    else
    {
        document.getElementById("divFormContent").innerHTML=value;
    }
    
    value=Portal.SODC.WebSite.UpdateVote.GetQuestion(language,VoteId).value;
     if(value.indexOf("ERROR")!=-1)
    {
        alert(value);
        return;
    }
    else
    {
        document.getElementById("txtVote").value=value;
    }
}


function ClientVoteFunc(TabModuleId) {
    Max = document.getElementById("divMaxVote_" + TabModuleId).value;
    for (var iIndex = 0; iIndex < Max; iIndex++) {
        checked = document.getElementById("checkVote_" + TabModuleId + "_" + iIndex).checked;
        if (checked) {
            ItemId = document.getElementById("txtVoteId_" + TabModuleId + "_" + iIndex).value;
            Subject = document.getElementById("txtVoteName_" + TabModuleId + "_" + iIndex).innerHTML;
            retValue = Portal.CDSP.Vote.IncVoteItem(ItemId).value;
            if (retValue.indexOf("ERROR") != -1) {
                alert(retValue);
                return;
            }
            alert(Portal.CDSP.Vote.GetVoteClicked(Subject).value);
            break;
        }
    }
}

function ResultVoteFunc(TabModuleId) {

    Url = Portal.CDSP.Vote.ShowResultForm(TabModuleId).value;
    if (Url.indexOf("ERROR") != -1) {
        alert(Url);
        return;
    }
    if (Url.indexOf("/-vie/") != -1) {
        Url = Url.replace("/-vie/", "/");
        
    }
    //window.showModalDialog(Url, null, 'dialogHeight: 250px; dialogWidth: 650px; dialogTop: 10px; dialogLeft: 150px; edge: Raised; center: Yes; help: No; scroll: Yes; status: Yes;');
    window.open(Url, "_blank");
    //var result = window.showModalDialog("http://localhost:2230/Portal/Sites/sodc/vie/ResultVote.aspx", null, 'dialogHeight: 600; dialogWidth: 800px; dialogTop: 10px; dialogLeft: 150px; edge: Raised; center: Yes; help: No; scroll: Yes; status: Yes;');
    

}

function ChangeOptionVote(TabModuleId,CheckIndex)
 {
     document.getElementById("divVoteContent" + TabModuleId).innerHTML = Portal.CDSP.Vote.GetHtml(TabModuleId, CheckIndex).value;
 }
                             